// Initial File - Paul Jack


#include "threadbase.h"




/*!
 * ThreadBase::ThreadBase 
 *
 * Description: 
 *
 * @param *worker is of type WorkerInterface 
 * @param * name is of type const char 
 *
 * @returns None
 */
ThreadBase::ThreadBase(WorkerInterface *worker, const char * name)
{
    mWorker = worker;
    mName = name;
    running = false;
}




/*!
 * ThreadBase::~ThreadBase 
 *
 * Description: 
 *
 *
 * @returns None.
 */
ThreadBase::~ThreadBase()
{
    stop();
    wait();
}




/*!
 * ThreadBase::setWorkerInterface 
 *
 * Description: 
 *
 * @param *pWorker is of type WorkerInterface 
 *
 * @returns void 
 */
void ThreadBase::setWorkerInterface(WorkerInterface *pWorker)
{
    mWorker = pWorker;
}



/*!
 * ThreadBase::setName 
 *
 * Description: 
 *
 * @param * name is of type const char 
 *
 * @returns void 
 */
void ThreadBase::setName(const char * name)
{
    mName = name;
}

/// This method is intended to only be called by the worker thread while
/// inside the work method (since we don't need to reacquire the mutex.


/*!
 * ThreadBase::stopFromWorkerThread 
 *
 * Description: 
 *
 *
 * @returns void 
 */
void ThreadBase::stopFromWorkerThread()
{
    stop();
}



/*!
 * ThreadBase::msleep 
 *
 * Description: 
 *
 * @param long msecs is of type unsigned 
 *
 * @returns void 
 */
void ThreadBase::msleep(unsigned long msecs)
{
    QThread::msleep(msecs);
}



/*!
 * ThreadBase::usleep 
 *
 * Description: 
 *
 * @param long usecs is of type unsigned 
 *
 * @returns void 
 */
void ThreadBase::usleep(unsigned long usecs)
{
    QThread::usleep(usecs);
}




/*!
 * ThreadBase::run 
 *
 * Description: 
 *
 *
 * @returns void 
 */
void ThreadBase::run()
{
    bool result = 1;
    mMutexRunning.lock();
    running = true;
    mMutexRunning.unlock();

    if(mWorker)
    {
        mWorker->initialize();
        // Always run the first time.
        bool cont = true;
        while(result && cont)
        {
            // grab the lock so no one can stop us while we run.
            mMutexRunning.lock();
            // find out if we were stopped last time.
            cont = running;
            // unlock it.
            mMutexRunning.unlock();
            if(cont)
            {
                // do the work.
                result = mWorker->work();
            }
            else
            {
                

            }
        }
    }
    else
    {
        // Always run the first time.
        bool cont = true;
        while(result && cont)
        {
            // grab the lock so no one can stop us while we run.
            mMutexRunning.lock();
            // find out if we were stopped last time.
            cont = running;
            mMutexRunning.unlock();
            if(cont)
            {
                // Since we don't have a worker thread we assume that
                // some class has overloaded the work method. So do
                // the work.
                work();
            }
            // unlock it.
        }
    }

 
    qDebug("ThreadBase::%s is exiting because of %s", 
            mName.toAscii().constData(), result ? 
            "continue not true" :"result not true");
    
//    // assert if we're still running.
//    if(running)
//    {
//        Q_ASSERT(false);
//    }
}





/*!
 * ThreadBase::work 
 *
 * Description: 
 *
 *
 * @returns bool 
 */
bool ThreadBase::work()
{
    // this should always be implemented if not using a RWorkerInterface.
    Q_ASSERT(false);
    return 1;
}
